package com.fuyun.microservice.handler;

import com.fuyun.microservice.connect.SessionManager;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChannelStateHandler extends ChannelDuplexHandler {

    private final SessionManager sessionManager;

    public ChannelStateHandler(SessionManager sessionManager){
        this.sessionManager = sessionManager;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.debug("active:{}",ctx.channel());
//        endpointManager.addChannel(ctx.channel());
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        log.debug("inactive:{}",ctx.channel());
        sessionManager.removeChannel(ctx.channel());
//        endpointManager.removeChannel(ctx.channel());
        ctx.fireChannelInactive();
    }
}
